from django.db import models
from django.contrib.auth import  get_user_model
from django.db.models import  CharField
from multiselectfield import MultiSelectField
from .choices import *

User =get_user_model()


class Order(models.Model):
    objects = models.Manager()
    seller= models.ForeignKey(User,on_delete=models.PROTECT)
    doctor = models.CharField(max_length=20)
    hospital = models.CharField(max_length=100)
    department = models.CharField(max_length=100)
    patient = models.CharField(max_length=20)
    male = models.BooleanField()
    phone = models.CharField(max_length=15)
    business_type = models.CharField(max_length=2,choices=BusinessType)
    sample_date = models.DateField()
    need_mail = models.BooleanField()
    mail_pic = models.ImageField(upload_to='data/mail_pictures', null=True)
    product = CharField(choices=Product, max_length=2)
    other_product = models.CharField(max_length=100, null=True)
    sample_type = MultiSelectField(choices=SAMPLE_TYPE, max_choices=2, max_length=20)
    check_order_pic = models.ImageField(upload_to="data/check_order_pictures")
    order_comment = models.TextField(max_length=1000)
    diagnosis = models.TextField(max_length=1000)
    price = models.FloatField()
    has_discount = models.BooleanField()
    discount = models.FloatField(default=0, null=True)
    payee = models.CharField(max_length=10)
    payment_method = models.CharField(choices=PaymentMethod, max_length=1, null=True)
    payment_date = models.DateField()
    payment_comment = models.TextField(max_length=1000)
    need_invoice = models.BooleanField(default=False)
    invoice_head = models.CharField(max_length=20)
    invoice_comment = models.TextField(max_length=1000)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        return "_".join([str(self.id), self.patient, self.seller.username, self.create_time.strftime("%Y-%m-%d %H:%M:%S")])


class State(models.Model):
    order = models.OneToOneField(Order,on_delete=models.CASCADE)
    state = models.CharField(choices=OrderState, max_length=1, default=OrderState.PENDING)
    comment = models.TextField(max_length=1000, null=True)
    report = models.FileField(upload_to="data/reports",  null=True)
    operator = models.ForeignKey(User, on_delete=models.PROTECT, null=True)

    def __str__(self):
        return "_".join([self.state,str(self.order)])







